package com.carleasoft.mps.cms.business.article.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.cms.business.article.bean.entity.*;
import com.carleasoft.mps.cms.business.article.bean.po.CollectorPo;
import com.carleasoft.mps.cms.business.article.bean.vo.*;
import com.carleasoft.mps.cms.business.article.constants.*;
import com.carleasoft.mps.cms.business.article.dao.ArticleDao;
import com.carleasoft.mps.cms.business.article.service.*;
import com.carleasoft.mps.cms.business.article.bean.po.ArticlePagePo;
import com.carleasoft.mps.cms.business.article.bean.po.ArticlePo;
import com.carleasoft.mps.cms.business.article.bean.po.ArticleStatusPo;
import com.carleasoft.mps.cms.business.collector.bean.entity.CollectorEntity;
import com.carleasoft.mps.cms.business.collector.service.CollectorService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.ConvertUtils;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private ArticleAccessoryService articleAccessoryService;

    @Autowired
    private ArticleDataService articleDataService;

    @Autowired
    private ArticleOperatingRecordService articleOperatingRecordService;
    @Autowired
    private ArticleService articleService;


    @Autowired
    private CollectorService collectorService;
    @Autowired
    private CategoryService categoryService;

    //来源
    private final Integer source = 0;

    /**
     * 分页查询:采编,校审,内容发布统一用一个
     *
     * @param page,ArticlePageVo
     * @param articlePagePo
     * @return
     */
    @Override
    public Page<ArticlePageVo> pageQuery(Page page, ArticlePagePo articlePagePo) {
        if (articlePagePo.getModule() == null) {
            throw new RuntimeException("指定查询列表");
        }
        if (StrUtil.isNotBlank(articlePagePo.getCategoryCode())) {
            String categoryCode = articlePagePo.getCategoryCode();
            //查询栏目表
            AuthorityCategoryEntity categoryEntity = categoryService.getOne(Wrappers.<AuthorityCategoryEntity>lambdaQuery()
                    .eq(AuthorityCategoryEntity::getCategoryCode, categoryCode));

            List<AuthorityCategoryEntity> categoryEntities = categoryService
                    .list(Wrappers.<AuthorityCategoryEntity>lambdaQuery()
                            .like(AuthorityCategoryEntity::getFullPath, categoryEntity.getCategoryCode()));

            List<String> codes = categoryEntities.stream()
                    .map(AuthorityCategoryEntity::getCategoryCode).collect(Collectors.toList());
            articlePagePo.setCategoryCodes(codes);
        }
        return articleDao.pageQuery(page, articlePagePo);
    }

    /**
     * 文章新增
     *
     * @param articlePo
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int insertArticle(ArticlePo articlePo) {
        //  获取用户信息
        UserCacheData loginUserInfo = ThreadUserContext.getCurrentCacheUser();
        int result = 0;
        //字段校验
        this.articleException(articlePo);
        int TitleLength = 80;
        if (articlePo.getTitle().length() > TitleLength) {
            throw new BusinessException(ExceptionCMSEnum.TITLE_LENGTH);
        }
        if (articlePo.getArticleModel() == null) {
            //设置默认排版样式
            articlePo.setArticleModel(ArticleModel.DOWN.getId());
        }
        //生成文章编码
        articlePo.setArticleCode(UUID.randomUUID().toString().replaceAll("-", ""));
        //设置文章状态
        AuthorityArticleEntity authorityArticleEntity = ConvertUtils.sourceToTarget(articlePo, AuthorityArticleEntity.class);
        authorityArticleEntity.setSource(articlePo.getSource().toString());
        authorityArticleEntity.setIsTop(false);
        if (!ContentModelEnum.POLICIES_REGULATIONS.getId().equals(authorityArticleEntity.getContentModel())) {
            authorityArticleEntity.setPrintDate(null);
            authorityArticleEntity.setPostShowName(null);
            authorityArticleEntity.setSourceOffice(null);
        }

        if (ContentModelEnum.NOTIFICATION_ANNOUNCEMENT.getId().equals(authorityArticleEntity.getContentModel()) || ContentModelEnum.POLICIES_REGULATIONS.getId().equals(authorityArticleEntity.getContentModel())) {
            authorityArticleEntity.setImagePath(null);
        }
        authorityArticleEntity.setArticleStatus(StatusEnum.NORMAL.getId());
        if (!FlowStatus.isInclude(articlePo.getFlowStatus())) {
            throw new BusinessException(ExceptionCMSEnum.PARAMETER_INVALID);
        }
        authorityArticleEntity.setFlowStatus(articlePo.getFlowStatus());

        //保存文章内容
        if (StrUtil.isNotBlank(articlePo.getArticleData())) {
            if (result == articleDataService.insertArticleData(ConvertUtils.sourceToTarget(articlePo, ArticleDataEntity.class))) {
                return result;
            }
            ;
        }
        //保存操作日志/只有一条
        ArticleOperationRecordEntity operationRecordEntity = this.insertOperatingRecord(loginUserInfo, articlePo);
        articleOperatingRecordService.insertOperatingRecord(operationRecordEntity);
        if (!articlePo.getFlowStatus().equals(FlowStatus.CREATE.getId())) {
            ArticleOperationRecordEntity recordEntity = IBeanUtil.clone(operationRecordEntity);
            recordEntity.setRecordType(FlowStatus.CREATE.getId());
            if (result == articleOperatingRecordService.insertOperatingRecord(recordEntity)) {
                return result;
            }
        }
        //保存附件
        articleAccessoryService.insertArticleFile(articlePo);
        //保存文章信息
        authorityArticleEntity.setWeight(0);
        if (FlowStatus.PUBLISH.getId().equals(authorityArticleEntity.getFlowStatus())) {
            authorityArticleEntity.setIssueBy(loginUserInfo.getUserName());
            authorityArticleEntity.setIssueTime(LocalDateTime.now());
        }
        authorityArticleEntity.setCollector(2);
        result = articleDao.insert(authorityArticleEntity);

        return result;
    }

    public ArticleOperationRecordEntity insertOperatingRecord(UserCacheData loginUserInfo, ArticlePo articlePo) {
        ArticleOperationRecordEntity operationRecordEntity = new ArticleOperationRecordEntity();
        operationRecordEntity.setRecordBy(loginUserInfo.getUserName());
        operationRecordEntity.setRecordTime(LocalDateTime.now());
        operationRecordEntity.setArticleCode(articlePo.getArticleCode());
        operationRecordEntity.setRecordType(articlePo.getFlowStatus());
        return operationRecordEntity;
    }

    /**
     * 文章修改
     *
     * @param articlePo
     */
    @Override
    @Transactional
    public int updateArticle(ArticlePo articlePo) {
        //  获取用户信息
        UserCacheData loginUserInfo = ThreadUserContext.getCurrentCacheUser();
        int result = 0;
        this.articleException(articlePo);//字段校验

        //正文修改
        AuthorityArticleEntity authorityArticleEntity = ConvertUtils.sourceToTarget(articlePo, AuthorityArticleEntity.class);
        authorityArticleEntity.setSource(articlePo.getSource().toString());
        AuthorityArticleEntity oldAuthorityArticleEntity = articleDao.selectOne(Wrappers.<AuthorityArticleEntity>lambdaQuery().eq(AuthorityArticleEntity::getArticleCode, articlePo.getArticleCode()));
        if (!authorityArticleEntity.getFlowStatus().equals(oldAuthorityArticleEntity.getFlowStatus())) {
            ArticleOperationRecordEntity articleOperationRecordEntity = this.insertOperatingRecord(loginUserInfo, articlePo);
            articleOperatingRecordService.insertOperatingRecord(articleOperationRecordEntity);
        }
        //详情修改
        if (StringUtils.isBlank(authorityArticleEntity.getArticleCode())) {
            throw new BusinessException(ExceptionCMSEnum.CATEGORY_CODE);
        }
        LambdaUpdateWrapper<AuthorityArticleEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(AuthorityArticleEntity::getArticleCode, authorityArticleEntity.getArticleCode());

        if (!StringUtils.isBlank(articlePo.getArticleData())) {
            ArticleDataEntity articleDataEntity = new ArticleDataEntity();
            articleDataEntity.setArticleCode(articlePo.getArticleCode());
            articleDataEntity.setArticleData(articlePo.getArticleData());
            result = articleDataService.updateByCode(articleDataEntity);
        }
        //附件修改
        articleAccessoryService.updateFileByCode(articlePo);
        result = articleDao.update(authorityArticleEntity, wrapper);
//        SearchAllPo push = push(articlePo.getArticleCode());
//        articleSearchService.editArticle(push,BaseConstants.INNER);
        return result;
    }

    /**
     * 详情查询
     *
     * @param articleCode
     * @return
     */
    @Override
    public ArticleVo findByIdArticle(String articleCode, String eip) {

        if (StringUtils.isBlank(articleCode)) {
            throw new BusinessException(ExceptionCMSEnum.ARTICLE_CODE);
        }
        LambdaQueryWrapper<AuthorityArticleEntity> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        AuthorityArticleEntity authorityArticleEntity = articleDao.selectOne(objectLambdaQueryWrapper
                .in(AuthorityArticleEntity::getArticleCode, articleCode));
        if (Objects.nonNull(authorityArticleEntity)) {
            //栏目
            AuthorityCategoryEntity categoryEntity = categoryService.selectByCode(authorityArticleEntity.getCategoryCode());
            List<ArticleOperationRecordEntity> recordEntity = null;
            if (StrUtil.isBlank(eip)) {
                //日志
                recordEntity = articleOperatingRecordService.findByCode(articleCode);
            }
            //正文
            ArticleDataEntity articleDataEntity = articleDataService.selectOne(articleCode);
            //附件
            List<ArticleAccessoryEntity> accessoryEntity = articleAccessoryService.findByCode(articleCode);


            ArticleVo articleVo = ConvertUtils.sourceToTarget(authorityArticleEntity, ArticleVo.class);
            articleVo.setSource(Integer.parseInt(authorityArticleEntity.getSource()));
            articleVo.setFullPath(categoryEntity.getFullPath());
            articleVo.setCategoryName(categoryEntity.getCategoryName());
            if (articleDataEntity != null && StrUtil.isNotBlank(articleDataEntity.getArticleData())) {
                if (eip!=null){
                    String html = articleDataEntity.getArticleData();
                    Document doc = Jsoup.parse(html);
                    Elements imgs = doc.select("img");
                    if (imgs.size() > 0) {
                        for (int i = 0; i < imgs.size(); i++) {
                            Element img = imgs.get(i);
                            String fileName = img.attr("alt");
                            String style = null;
                            String attr = img.attr("style");
                            if (StringUtils.isBlank(attr)) {
                                style = "width: 200px; height: 200px";
                            } else {
                                style = img.attr("style");
                            }
                            String result = "<ArmyImg fileName=\"" + fileName + "\" style=\"" + style + "\"></ArmyImg>";

                            if (html.contains(fileName)) {
                                html = html.replaceFirst("<img[^>]+>", result);
                            }
                        }
                        articleVo.setArticleData(html);
                    }
                    articleVo.setArticleData(articleDataEntity.getArticleData());
                }else {
                    articleVo.setArticleData(articleDataEntity.getArticleData());

                }
            }
            articleVo.setArticleOperationRecordEntityList(recordEntity);
            articleVo.setArticleAccessoryEntityList(accessoryEntity);

            return articleVo;
        }
        return null;
    }

    @Override
    @Transactional
    public int updateArticleStatus(ArticleStatusPo articleStatusPo) {
        //  获取用户信息
        UserCacheData loginUserInfo = ThreadUserContext.getCurrentCacheUser();
        int result = 0;
        //修改状态
        if (StringUtils.isBlank(articleStatusPo.getArticleCode())) {
            throw new BusinessException(ExceptionCMSEnum.CATEGORY_CODE);
        }
        if (articleStatusPo.getArticleStatus() == null && articleStatusPo.getFlowStatus() == null) {
            throw new BusinessException(ExceptionCMSEnum.FLOW_STATUS_NULL);
        }
        AuthorityArticleEntity authorityArticleEntity = ConvertUtils.sourceToTarget(articleStatusPo, AuthorityArticleEntity.class);
        if (articleStatusPo.getFlowStatus() != null && articleStatusPo.getFlowStatus().equals(FlowStatus.PUBLISH.getId())) {
            authorityArticleEntity.setIssueBy(loginUserInfo.getUserName());
            authorityArticleEntity.setIssueTime(LocalDateTime.now());

        }
        result = 1;
        //新增操作
        if (articleStatusPo.getFlowStatus() != null) {
            ArticleOperationRecordEntity recordEntity = new ArticleOperationRecordEntity();
            recordEntity.setArticleCode(articleStatusPo.getArticleCode());
            recordEntity.setRecordBy(loginUserInfo.getUserName());
            recordEntity.setRecordTime(LocalDateTime.now());
            recordEntity.setRecordType(articleStatusPo.getFlowStatus());
            recordEntity.setRecordRemark(articleStatusPo.getRecordRemark());
            result = articleOperatingRecordService.insertOperatingRecord(recordEntity);
        }
        result = articleDao.update(authorityArticleEntity, Wrappers.<AuthorityArticleEntity>lambdaUpdate()
                .eq(AuthorityArticleEntity::getArticleCode, authorityArticleEntity.getArticleCode()));


        return result;
    }

    @Override
    public Page<SlideshowEIPVo> EIPPageQuery(Page<ArticlePo> page, ArticlePo articlePo) {
        articlePo.setResource(1);
        if (StrUtil.isNotBlank(articlePo.getCategoryCode())) {
            String categoryCode = articlePo.getCategoryCode();
            //查询栏目表
            AuthorityCategoryEntity categoryEntity = categoryService.getOne(Wrappers.<AuthorityCategoryEntity>lambdaQuery()
                    .eq(AuthorityCategoryEntity::getCategoryCode, categoryCode));

            List<AuthorityCategoryEntity> categoryEntities = categoryService
                    .list(Wrappers.<AuthorityCategoryEntity>lambdaQuery()
                            .like(AuthorityCategoryEntity::getFullPath, categoryEntity.getCategoryCode()));

            List<String> codes = categoryEntities.stream()
                    .map(AuthorityCategoryEntity::getCategoryCode).collect(Collectors.toList());
            articlePo.setCategoryCodes(codes);
        }
        Page<SlideshowEIPVo> approvalModulePageVos = articleDao.EIPPageQuery(page, articlePo);
        if (CollUtil.isNotEmpty(approvalModulePageVos.getRecords())) {
            List<String> categoryIds = approvalModulePageVos.getRecords().stream().map(SlideshowEIPVo::getCategoryCode).collect(Collectors.toList());
            List<AuthorityCategoryEntity> list = categoryService.list(Wrappers.<AuthorityCategoryEntity>lambdaQuery().in(AuthorityCategoryEntity::getCategoryCode, categoryIds));
            Map<String, AuthorityCategoryEntity> categoryEntityMap = list.stream().collect(Collectors.toMap(AuthorityCategoryEntity::getCategoryCode, Function.identity()));
            approvalModulePageVos.getRecords().forEach(item -> {
                item.setCategoryName(categoryEntityMap.get(item.getCategoryCode()).getCategoryName());
            });
        }
        Iterator<SlideshowEIPVo> iter = approvalModulePageVos.getRecords().iterator();
        while (iter.hasNext()){
            SlideshowEIPVo vo = iter.next();
            if(StringUtils.isNotBlank(articlePo.getArticleAccessoryEntityList())){
                List<ArticleAccessoryEntity> accessoryEntities = articleAccessoryService.findByCodeAndFileName(vo.getArticleCode(),articlePo.getArticleAccessoryEntityList());
                if(CollUtil.isEmpty(accessoryEntities)){
                    iter.remove();
                }
            }
        }
        return approvalModulePageVos;
    }

    @Override
    public EipIndexOf eipUpAndDown(Page page, ArticlePo articlePo) {
        articlePo.setResource(2);
        AuthorityArticleEntity articleEntity = articleDao.selectOne(Wrappers.<AuthorityArticleEntity>lambdaQuery()
                .eq(AuthorityArticleEntity::getArticleCode, articlePo.getArticleCode()));
        articlePo.setCode(articleEntity.getSiteCode());
        Page<SlideshowEIPVo> approvalModulePageVos = articleDao.EIPPageQuery(page, articlePo);
        if (CollUtil.isNotEmpty(approvalModulePageVos.getRecords())) {
            List<String> categoryIds = approvalModulePageVos.getRecords().stream().map(SlideshowEIPVo::getCategoryCode).collect(Collectors.toList());
            List<AuthorityCategoryEntity> list = categoryService.list(Wrappers.<AuthorityCategoryEntity>lambdaQuery().in(AuthorityCategoryEntity::getCategoryCode, categoryIds));
            Map<String, AuthorityCategoryEntity> categoryEntityMap = list.stream().collect(Collectors.toMap(AuthorityCategoryEntity::getCategoryCode, Function.identity()));
            approvalModulePageVos.getRecords().forEach(item -> {
                item.setCategoryName(categoryEntityMap.get(item.getCategoryCode()).getCategoryName());
            });
        }
        EipIndexOf eipIndexOf = new EipIndexOf();
        if (CollUtil.isNotEmpty(approvalModulePageVos.getRecords())) {
            List<String> articleCodes = new ArrayList<>();
            approvalModulePageVos.getRecords().forEach(item -> {
                articleCodes.add(item.getArticleCode());
            });
            int last = articleCodes.size();
            int index = articleCodes.indexOf(articlePo.getArticleCode());
            if (index!=-1){
                if (index == 0) {
                    eipIndexOf.setUpCode(null);
                    if (index == last - 1) {
                        eipIndexOf.setDownCode(null);
                    } else {
                        SlideshowEIPVo slideshowEIPVo1 = new SlideshowEIPVo();
                        slideshowEIPVo1.setArticleCode(articleCodes.get(index + 1));
                        eipIndexOf.setDownCode(slideshowEIPVo1);
                    }
                } else {
                    SlideshowEIPVo slideshowEIPVo2 = new SlideshowEIPVo();
                    slideshowEIPVo2.setArticleCode(articleCodes.get(index - 1));
                    eipIndexOf.setUpCode(slideshowEIPVo2);
                    if (index == last - 1) {
                        eipIndexOf.setDownCode(null);
                    } else {
                        SlideshowEIPVo slideshowEIPVos = new SlideshowEIPVo();
                        slideshowEIPVos.setArticleCode(articleCodes.get(index + 1));
                        eipIndexOf.setDownCode(slideshowEIPVos);
                    }
                }
                ArticleVo current = articleService.findByIdArticle(articlePo.getArticleCode(), "eip");
                SlideshowEIPVo currentInfo = BeanUtil.copyProperties(current, SlideshowEIPVo.class);
                eipIndexOf.setCurrentCode(currentInfo);
                if (Objects.nonNull(eipIndexOf.getDownCode())) {
                    ArticleVo down = articleService.findByIdArticle(eipIndexOf.getDownCode().getArticleCode(), "eip");
                    SlideshowEIPVo downInfo = BeanUtil.copyProperties(down, SlideshowEIPVo.class);
                    eipIndexOf.setDownCode(downInfo);
                }
                if (Objects.nonNull(eipIndexOf.getUpCode())) {
                    ArticleVo up = articleService.findByIdArticle(eipIndexOf.getUpCode().getArticleCode(), "eip");
                    SlideshowEIPVo upInfo = BeanUtil.copyProperties(up, SlideshowEIPVo.class);
                    eipIndexOf.setUpCode(upInfo);
                }
            }



        }
        return eipIndexOf;
    }

    @Override
    public int count(String categoryCode) {
        return articleDao.selectCount(Wrappers.<AuthorityArticleEntity>lambdaQuery()
                .eq(AuthorityArticleEntity::getCategoryCode, categoryCode)
                .eq(AuthorityArticleEntity::getFlowStatus, 3)
        .eq(AuthorityArticleEntity::getArticleStatus,0));
    }

    @Override
    public boolean collector(CollectorPo collectorPo) {
        boolean isOk = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        IBeanUtil.paramVerify(collectorPo, "code", "collector", "type", "collector");
        AuthorityArticleEntity authorityArticleEntity = articleDao.selectOne(Wrappers.<AuthorityArticleEntity>lambdaQuery()
                .eq(AuthorityArticleEntity::getArticleCode, collectorPo.getCode()));
        authorityArticleEntity.setCollector(collectorPo.getCollector());
         articleDao.updateById(authorityArticleEntity);
        CollectorEntity collectorEntity = new CollectorEntity();
        collectorEntity.setCode(collectorPo.getCode());
        collectorEntity.setUserCode(user.getUserCode());
        collectorEntity.setUserName(user.getUserName());
        if (collectorPo.getCollector() == 1) {
            collectorEntity.setName(authorityArticleEntity.getTitle());
            collectorEntity.setCreateTime(LocalDateTime.now());
            collectorEntity.setType(collectorPo.getType());
            isOk = collectorService.saveCollector(collectorEntity);
        } else {
            isOk = collectorService.delete(collectorEntity);
        }

        return isOk;
    }

    @Override
    public int toTop(AuthorityArticleEntity authorityArticleEntity) {
        //判断是不是发布状态
        Integer result = 0;
        AuthorityArticleEntity entity = articleDao.selectOne(Wrappers.<AuthorityArticleEntity>lambdaQuery()
                .eq(AuthorityArticleEntity::getArticleCode, authorityArticleEntity.getArticleCode()));
        if (entity == null) {
            return result;
        }
        if (!FlowStatus.PUBLISH.getId().equals(entity.getFlowStatus())) {
            throw new BusinessException(ExceptionCMSEnum.TO_TOP);
        }
        if (ContentModelEnum.SLIDESHOW.getId() .equals(entity.getContentModel())) {
            throw new BusinessException(ExceptionCMSEnum.SLIDESHOW_TOP);
        }
        if (authorityArticleEntity.getIsTop()) {
            result = articleDao.update(null, Wrappers.<AuthorityArticleEntity>lambdaUpdate()
                    .set(AuthorityArticleEntity::getIsTop, authorityArticleEntity.getIsTop())
                    .set(AuthorityArticleEntity::getTopTime, LocalDateTime.now())
                    .eq(AuthorityArticleEntity::getArticleCode, authorityArticleEntity.getArticleCode()));
        } else {
            //取消置顶
            result = articleDao.update(null, Wrappers.<AuthorityArticleEntity>lambdaUpdate()
                    .set(AuthorityArticleEntity::getIsTop, false)
                    .set(AuthorityArticleEntity::getTopTime, null)
                    .eq(AuthorityArticleEntity::getArticleCode, authorityArticleEntity.getArticleCode()));
        }
        return result;
    }

    @Override
    public int updateWeight(AuthorityArticleEntity authorityArticleEntity) {
        LambdaUpdateWrapper<AuthorityArticleEntity> lambdaUpdate = Wrappers.lambdaUpdate();
        lambdaUpdate.set(AuthorityArticleEntity::getWeight, authorityArticleEntity.getWeight())
                .eq(AuthorityArticleEntity::getArticleCode, authorityArticleEntity.getArticleCode());
        return articleDao.update(null, lambdaUpdate);
    }

    @Override
    public List<SlideshowEIPVo> homePage(String categoryCode) {
        //查询栏目最底层
        categoryCode = categoryService.findByParentCode(categoryCode);
        return articleDao.homePage(categoryCode);
    }

    @Override
    @Deprecated
    public List<EIPDetailVo> slideshowPage(String categoryCode) {
        LambdaQueryWrapper<AuthorityArticleEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AuthorityArticleEntity::getContentModel, ContentModelEnum.SLIDESHOW.getId())
                .eq(AuthorityArticleEntity::getCategoryCode, categoryCode)
                .eq(AuthorityArticleEntity::getFlowStatus, FlowStatus.PUBLISH.getId())
                .eq(AuthorityArticleEntity::getArticleStatus, StatusEnum.NORMAL.getId())
                .select(AuthorityArticleEntity::getImagePath, AuthorityArticleEntity::getArticleCode, AuthorityArticleEntity::getTitle, AuthorityArticleEntity::getJumpLink)
                .orderByDesc(AuthorityArticleEntity::getIssueTime)
                .last("limit 7");
        List<AuthorityArticleEntity> entityList = articleDao.selectList(wrapper);
        if (entityList != null && entityList.size() > 0) {
            List<String> categoryList = entityList.stream().map(AuthorityArticleEntity::getArticleCode).collect(Collectors.toList());
            List<EIPDetailVo> eipDetailVos = ConvertUtils.sourceToTarget(entityList, EIPDetailVo.class);
            Map<String, ArticleDataEntity> entityMap = articleDataService.findByList(categoryList).stream().collect(Collectors.toMap(ArticleDataEntity::getArticleCode, Function.identity()));
            if (entityMap != null && entityMap.size() > 0) {
                eipDetailVos.stream().forEach(item -> {
                    item.setArticleData(entityMap.get(item.getArticleCode()).getArticleData());
                });
            }
            return eipDetailVos;
        }

        return null;
    }

    @Override
    public Page<SlideshowEIPVo> topPageQuery(Page page, ArticlePo articlePo) {
        return articleDao.topPageQuery(page, articlePo);
    }


    public void articleException(ArticlePo articlePo) {
        if (StrUtil.isNotBlank(articlePo.getProcessMode())) {
            switch (ArticleProcessMode.valueOf(articlePo.getProcessMode())) {
                case CUSTOM:
                    if ((ContentModelEnum.ARTICLE.getId().equals(articlePo.getContentModel())
                            || ContentModelEnum.NOTIFICATION_ANNOUNCEMENT.getId().equals(articlePo.getContentModel())) && StrUtil.isBlank(articlePo.getDigest())) {
                        throw new BusinessException(ExceptionCMSEnum.DIGEST_NULL);
                    }
                    if (StrUtil.isNotBlank(articlePo.getDigest()) && articlePo.getDigest().length() > 200) {
                        throw new BusinessException(ExceptionCMSEnum.CHARS_OVER_LENGTH);
                    }
                    if (StringUtils.isBlank(articlePo.getArticleData())) {
                        throw new BusinessException(ExceptionCMSEnum.ARTICLE_DATE_NULL);
                    }


                    break;
                case JUMP:
                    if (StrUtil.isBlank(articlePo.getJumpLink())) {
                        throw new BusinessException(ExceptionCMSEnum.JUMP_LINK);
                    }
                    break;
            }
        }

        if (StringUtils.isBlank(articlePo.getTitle())) {
            throw new BusinessException(ExceptionCMSEnum.TITLE_NULL);
        }
        if (StringUtils.isBlank(articlePo.getCategoryCode())) {
            throw new BusinessException(ExceptionCMSEnum.CATEGORY_CODE);
        }
    }


}
